package Day012_多线程2.ZuoYe;

import java.util.concurrent.Exchanger;

/**
 * @author zhangyan
 * @date 2020/4/30 18:11
 */
public class Test3_7 {
    public static void main(String[] args) {
        //创建对象
        Exchanger<String> ex= new Exchanger<>();
        //创建执行任务
        MyRunnable_A mra = new MyRunnable_A(ex);
        MyRunnable_B mrb = new MyRunnable_B(ex);
        new Thread(mra).start();
        new Thread(mrb).start();
    }
}

class MyRunnable_A implements Runnable{
    //
    Exchanger<String> ex;
    public MyRunnable_A(Exchanger<String> ex){
        this.ex = ex;
    }
    @Override
    public void run() {
        try {
            String massageB = ex.exchange("一条体育新闻");
            System.out.println("线程A给线程B:" + massageB);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class MyRunnable_B implements Runnable{
    //
    Exchanger<String> ex;
    public MyRunnable_B(Exchanger<String> ex){
        this.ex = ex;
    }
    @Override
    public void run() {
        try {
            String massageA = ex.exchange("一条娱乐新闻");
            System.out.println("线程B给线程A:" + massageA);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}